'''
Created on Apr 25, 2012

@author: mshen
'''
import json
import datetime
# import the REST library
from restful_lib import Connection


class ReachOutHttpClient():

    def __init__(self, base_url = "http://localhost:8080/ReachOut/rest/community"):
        self.base_url = base_url
        self.conn = Connection(base_url)
        
    def setRootURL(self, URL):
        self.base_url = URL
        self.conn = Connection(URL)
    
    def createCharity(self, charityObject):
        resp = self.conn.request_put("/charity", body=json.dumps(charityObject, default=lambda o: o.__dict__))
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return "Charity successfully created."
        else:
            return 'Error status code: ' + statusCode
        
    def listCharities(self):
        resp = self.conn.request_get("/charity", headers={"Content-Type":"application/xml"})
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return resp[u'body']
        else:
            return 'Error status code: ' + statusCode

    def getCharityByName(self, charityName):
        resp = self.conn.request_get("/charity/" + charityName, headers={"Accept":"application/xml"})
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return resp[u'body']
        else:
            return 'Error status code: ' + statusCode
        
    def donateCharity(self, charityObject):
        resp = self.conn.request_post("/charity/donate", body=json.dumps(charityObject, default=lambda o: o.__dict__))
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return resp[u'body']
        else:
            return 'Error status code: ' + statusCode
      
    def listDiscussion(self):  
        resp = self.conn.request_get("/discussionboard", headers={"Accept":"application/xml"})
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return resp[u'body']
        else:
            return 'Error status code: ' + statusCode

    def createTopic(self, topic):
        resp = self.conn.request_put("/discussionboard", body=json.dumps(topic, default=lambda o: o.__dict__))
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return "Topic successfully created."
        else:
            return 'Error status code: ' + statusCode

    def createComment(self, comment):
        resp = self.conn.request_put("/discussionboard/comment", body=json.dumps(comment, default=lambda o: o.__dict__))
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return "Comment successfully added."
        else:
            return 'Error status code: ' + statusCode
        
    def getTopicByName(self, topicName):
        resp = self.conn.request_get("/discussionboard/1/" + topicName, headers={"Accept":"application/xml"})
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
#            return "Comment successfully added."
        else:
            return 'Error status code: ' + statusCode

    def getTopicDetails(self, topicName):
        resp = self.conn.request_get("/discussionboard/2/" + topicName, headers={"Accept":"application/xml"})
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
#            return "Comment successfully added."
        else:
            return 'Error status code: ' + statusCode

    def subscribe(self, sub):
        resp = self.conn.request_post("/subscribe/charity", headers={"Content-Type":"application/json"}, body=json.dumps(sub, default=lambda o: o.__dict__))
        
        statusCode = resp[u'headers']['status']
        
        if statusCode == '200':
            print resp[u'body']
            return resp[u'body']
        else:
            return 'Error status code: ' + statusCode
      
#        bc.demoServices();